package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-binary-tree/
 * @create 2025/9/15 22:55
 * @since JDK17
 */

public class Solution52 {
    int[] nums;
    TreeNode f(int l, int r) {
        if (l > r) {
            return null;
        }
        int max = l;
        for (int i = l + 1; i <= r; i++) {  // 找到最大值
            if (nums[max] < nums[i]) {
                max = i;
            }
        }
        return new TreeNode(nums[max], f(l, max - 1), f(max + 1, r));
    }

    public TreeNode constructMaximumBinaryTree(int[] nums) {
        this.nums = nums;
        return f(0, nums.length - 1);
    }
}
